<?php

/**
 * 后台管理控制器
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';
require_once('Zend/Filter.php');
require_once('User.php');

class AdminController extends Zend_Controller_Action {
	private $user;
	
	public function init() {
		session_start();
		if (isset($_SESSION['user']) && ($_SESSION['user']['login']=="Y")) {
			$this->view->useract = $_SESSION['user']['useract'];
			$this->view->column = $_SESSION['user']['column'];
			$this->view->username = $_SESSION['user']['username'];
			if (isset($_SESSION['user']['msg'])) {
				$this->view->msgSection = $_SESSION['user']['msg'];
			}
		} else {
			$this->_redirect("/login/");
		}
		$this->view->addHelperPath(VIEW_HELPER_PATH, 'My_View_Helper');
	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$this->chkLogin();
//		$col = $_SESSION['user']['column'];
//		print_r($col);
//		$act = $_SESSION['user']['useract'];
//		print_r($act);
//		$this->_helper->viewRenderer->setNoRender();
	}
	
	
	/***       登录等相关           ***/
	/**
	 * 显示登录页面
	 * @access public
	 * @return 
	 */
	public function loginAction(){
		session_unset();
		session_destroy();
		$this->_redirect("/login/");
	}
	/**
	 * 退出登录
	 * @access public
	 */
	public function logoutAction(){
		session_start();
		session_destroy();
		$this->_redirect('/admin');
	}
	/**
	 * 重新登录
	 * @access public
	 */
	public function reloginAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_redirect('/admin/logout');
	}	
	/**
	 * 检测是否登录,未登录则显示登录页面
	 * @access public
	 * @return void
	 */
	private function chkLogin(){
		if (isset($_SESSION['user']) && ($_SESSION['user']['login']=="Y")) {
			return ;
		}else {
			$this->_redirect("/login/");
		}
	}
	
	/**
	 * 
	 * @access public
	 * @return 
	 */
	public function __call($arg,$method){
		$this->chkLogin();
		$this->_redirect('/admin/index');
	}
	
	
	
	/******    角色操作相关    *****/
	/**
	 * 取角色列表
	 * @access public
	 * @return array
	 */
	private function getRoleList(){
		$role = new Role();
		$roleArr = $role->getAllRole();
		return $roleArr;
	}
	/**
	 * 显示添加角色页面
	 * @access public
	 * @return 
	 */
	public function addroleAction(){
		$this->view->roleArr = $this->getRoleList();
		
	}
	/**
	 * 新增角色
	 * @access public
	 * @return 
	 */
	public function insroleAction(){
		$role = $this->_getParam('role',null);
		$info = $this->_getParam('info',null);
		if (($role==null)||($info==null)) {
			$_SESSION['user']['msg'] = "请输入完整信息";
			$this->_redirect("/admin/addrole");
		}
		$roleArr = array('name'=>$role,'info'=>$info,'userid'=>$_SESSION['user']['username'],'username'=>$_SESSION['user']['userinfo']['truename'],'createdate'=>date('Y-m-d H:i:s'));
		$roleObj = new Role();
		$lastid = $roleObj->insRole($roleArr);
		$_SESSION['user']['msg'] = "添加成功!";
		$this->_redirect("/admin/addrole");
	}
	/**
	 * 显示修改角色页面
	 * @access public
	 * @return 
	 */
	public function updroleAction(){
		$this->roleChkParam();
		$roleid = $this->_getParam('id');
		$_SESSION['user']['msg'] = "";
		$roleObj = new Role();
		$roleArr = $roleObj->getRoleById($roleid);
		$this->view->roleArr = $roleArr;
	}
	/**
	 * 修改用户角色
	 * @access public
	 * @return 
	 */
	public function modiroleAction(){
		$this->roleChkParam();
		$id = $this->_getParam('id');
		$name = $this->_getParam('role',null);
		$info = $this->_getParam('info',null);
		if (($name == null)||($info == null)) {
			$_SESSION['user']['msg'] = "信息不全!";
			$this->_redirect("/admin/addrole");
		}
		$data = array('name'=>$name,'info'=>$info,'userid'=>$_SESSION['user']['username'],'username'=>$_SESSION['user']['userinfo']['truename'],'createdate'=>date('Y-m-d H:i:s'));
		$roleObj = new Role();
		$lastid = $roleObj->updRole($data,$id);
		$_SESSION['user']['msg'] = "修改成功！";
		$this->_redirect("/admin/addrole");
	}
	/**
	 * 删除角色
	 * @access public
	 * @return 
	 */
	public function delroleAction(){
		$this->roleChkParam();
		$roleid = $this->_getParam('id');
		$roleObj = new Role();
		$roleObj->delRole($roleid);
		$_SESSION['user']['msg'] = "删除成功";
		$this->_redirect("/admin/addrole");
	}
	/**
	 * 角色操作检测是否带参数ID
	 * @access private
	 * @return 
	 */
	private function roleChkParam(){
		if (!$this->_hasParam('id')) {
			$_SESSION['user']['msg'] = "没有角色信息";
			$this->_redirect('/admin/addrole');
		} else return ;
	}
	
	
	
	/***		用户操作相关		****/
	/**
	 * 显示添加用户页面
	 * @access public
	 * @return 
	 */
	public function useraddAction(){
		$this->getUserList();
	}
	/**
	 * 手动添加用户
	 * @access public
	 * @return 
	 */
	public function userinsAction(){
		
	}
	/**
	 * 修改用户页面
	 * @access public
	 * @return 
	 */
	public function userupdAction(){
		
	}
	/**
	 * 修改用户
	 * @access public
	 * @return 
	 */
	public function usermodiAction(){
		$this->_helper->viewRenderer->setNoRender();
		echo "不能删除用户";
	}
	/**
	 * 删除用户
	 * @todo 应不能删除
	 * @access public
	 * @return 
	 */
	public function userdelAction(){
		
	}
	/**
	 * 取用户列表
	 * @access private
	 * @return array
	 */
	private function getUserList(){
		$userObj = new User($_SESSION['user']['username']);
		$this->view->userArr = $userObj->getUserList();
	}
	
	
	
	/*****		动作操作相关			****/
	/**
	 * 显示新增动作页面
	 * @access public
	 * @return 
	 */
	public function addactAction(){
		$this->view->actArr = $this->getActList();
		$this->view->columnArr = $this->getColumn();
		$this->view->pageSction = $this->_getParam('p',0);
		$_SESSION['user']['msg'] = "";
	}
	/**
	 * 添加动作
	 * @access public
	 * @return 
	 */
	public function insactAction(){
		$action = $_REQUEST['act'];
		$name = $this->_getParam('name',null);
		$columnid = $this->_getParam('columnid',null);
		$viewmode = $this->_getParam('viewmode',null);
		if (($action == null)||($name == null)||($columnid==null)||($viewmode==null)) {
			$_SESSION['user']['msg'] = "信息不全，请重新输入";
			$this->_redirect('/admin/addact');
		}
		$data = array('action'=>$action,'name'=>$name,'columnid'=>$columnid,'viewmode'=>$viewmode);
		$actObj = new Action();
		$lastid = $actObj->insAct($data);
		$_SESSION['user']['msg'] = "添加成功!";
		$this->_redirect("/admin/addact");
	}
	/**
	 * 修改动作页面
	 * @access public
	 * @return 
	 */
	public function updactAction(){
		$this->actChkParam();
		$id = $this->_getParam('id');
		$actObj = new Action();
		$actArr = $actObj->getActById($id);
		$this->view->actArr = $actArr;
		$this->view->columnArr = $this->getColumn();
		$_SESSION['user']['msg'] = "";
	}
	/**
	 * 修改动作
	 * @access public
	 * @return 
	 */
	public function modiactAction(){
		$id = $_REQUEST['id'];
		$action = $_REQUEST['action'];
		$name = $_REQUEST['name'];
		$columnid = $_REQUEST['columnid'];
		$viewmode = $_REQUEST['viewmode'];
		$data = array('action'=>$action,'name'=>$name,'columnid'=>$columnid,'viewmode'=>$viewmode);
		$actObj = new Action();
		$cnt = $actObj->updAct($data,$id);
		$_SESSION['user']['msg']="修改成功";
		$this->_redirect("/admin/addact");
	}
	/**
	 * 删除动作页面
	 * @access public
	 * @return 
	 */
	public function delactAction(){
		$this->actChkParam();
		$id = $this->_getParam('id',null);
		$actObj = new Action();
		$lastid = $actObj->delAct($id);
		$_SESSION['user']['msg'] = "删除成功！";
		$this->_redirect("/admin/addact");
	}
	/**
	 * 动作操作检测是否有参数id
	 * @access public
	 * @todo 需检测是否有此ID
	 * @return void
	 */
	public function actChkParam(){
		if (!$this->_hasParam('id')) {
			$_SESSION['user']['msg'] = "缺少参数！";
			$this->_redirect("/admin/addact");
		} else return ;
	}
	/**
	 * 取所有动作
	 * @access private
	 * @return array 所有动作列表
	 */
	private function getActList(){
		$actObj = new Action();
		$actList = $actObj->getAllAct();
		return $actList;
	}
	/**
	 * 取显示顺序数组
	 * @access private
	 * @return array 显示顺序
	 */
	private  function getColumn(){
		$columnObj = new Column();
		$columnArr = $columnObj->getColumn();
		return $columnArr;
	}
	/**
	 * 显示角色所对应的动作页面
	 * @access public
	 * @return 
	 */
	public function useractAction(){
		
	}
	
	
	
	/***  以下为信息维护相关操作   ***/
	/**
	 * 显示所有公司信息页面
	 * @access public
	 * @return 
	 */
	public function complistAction(){
		$compObj = new Company();
		$allCompArr = $compObj->getAllCompany();
		$this->view->allCompArr = $allCompArr;
		$this->view->pageSection = $this->_getParam('p',0);
	}
	/**
	 * 显示新增页面
	 * @access public
	 * @return 
	 */
	public function compaddAction(){
		
	}
	/**
	 * 添加公司
	 * @access public
	 * @return 
	 */
	public function compinsAction(){
		$data['tablename'] = $_REQUEST['tablename'];
		$data['objid'] = $_REQUEST['objid'];
		$data['state'] = $_REQUEST['state'];
		$data['name'] = $_REQUEST['name'];
		
		$data['logopath'] = $_REQUEST['logopath'];
		$data['mappath'] = $_REQUEST['mappath'];
		$data['type'] = $_REQUEST['type'];
		$data['btype'] = $_REQUEST['btype'];
		$data['stype'] = $_REQUEST['stype'];

		$data['province'] = $_REQUEST['province'];
		$data['city'] = $_REQUEST['city'];
		$data['county'] = $_REQUEST['county'];
		$data['manageobj'] = $_REQUEST['manageobj'];
		$data['power'] = $_REQUEST['power'];
		$data['address'] = $_REQUEST['address'];
		$data['postcode'] = $_REQUEST['postcode'];
		
		$data['linkman'] = $_REQUEST['linkman'];
		$data['phone'] = $_REQUEST['phone'];
		$data['mobile'] = $_REQUEST['mobile'];
		$data['fax'] = $_REQUEST['fax'];
		$data['manager'] = $_REQUEST['manager'];
		$data['brief'] = $_REQUEST['brief'];
		
		$data['website'] = $_REQUEST['website'];
		$data['email'] = $_REQUEST['email'];
		$data['comment'] = $_REQUEST['comment'];
		$data['clickcnt'] = $_REQUEST['clickcnt'];
		$data['disbrief'] = $_REQUEST['disbrief'];
		$comObj = new Company();
		$insid = $comObj->insCompany($data);
		$_SESSION['user']['msg'] = "添加成功";
		$this->_redirect("/admin/complist");
	}
	/**
	 * 显示公司信息修改页面
	 * @access public
	 * @return 
	 */
	public function compupdAction(){
		$companyArr = $this->chkCompId();
		$this->view->companyArr = $companyArr;
	}
	/**
	 * 修改公司信息
	 * @access public
	 * @return 
	 */
	public function compmodiAction(){

		$id = $_REQUEST['id'];
		$data['tablename'] = $_REQUEST['tablename'];
		$data['objid'] = $_REQUEST['objid'];
		$data['state'] = $_REQUEST['state'];
		$data['name'] = $_REQUEST['name'];
		
		$data['logopath'] = $_REQUEST['logopath'];
		$data['mappath'] = $_REQUEST['mappath'];
		$data['type'] = $_REQUEST['type'];
		$data['btype'] = $_REQUEST['btype'];
		$data['stype'] = $_REQUEST['stype'];

		$data['province'] = $_REQUEST['province'];
		$data['city'] = $_REQUEST['city'];
		$data['county'] = $_REQUEST['county'];
		$data['manageobj'] = $_REQUEST['manageobj'];
		$data['power'] = $_REQUEST['power'];
		$data['address'] = $_REQUEST['address'];
		$data['postcode'] = $_REQUEST['postcode'];
		
		$data['linkman'] = $_REQUEST['linkman'];
		$data['phone'] = $_REQUEST['phone'];
		$data['mobile'] = $_REQUEST['mobile'];
		$data['fax'] = $_REQUEST['fax'];
		$data['manager'] = $_REQUEST['manager'];
		$data['brief'] = $_REQUEST['brief'];
		
		$data['website'] = $_REQUEST['website'];
		$data['email'] = $_REQUEST['email'];
		$data['comment'] = $_REQUEST['comment'];
		$data['clickcnt'] = $_REQUEST['clickcnt'];
		$data['disbrief'] = $_REQUEST['disbrief'];
		
		$comObj = new Company();
		$updCnt = $comObj->updCompany($data,$id);
		$_SESSION['msg'] = "修改成功";
		$this->_redirect("/admin/complist");
	}
	/**
	 * 删除公司信息
	 * @todo 公司信息不可删除
	 * @access public
	 * @return 
	 */
	public function compdelAction(){
		$this->chkCompId();
	}
	/**
	 * 检查公司ID
	 * @access private
	 * @return array 公司数组
	 */
	private function chkCompId(){
		$compId = $this->_getParam('id',null);
		if ($compId == null) {
			$_SESSION['msg'] = '请选择公司';
			$this->_redirect("/admin/complist");
		} else {
			$companyObj = new Company();
			$companyArr = $companyObj->getCompanyById($compId);
			if (empty($companyArr)) {
				$_SESSION['无此公司信息'];
				$this->_redirect("/admin/complist");
			}
			return $companyArr;
		}
	}
	
	/**
	 * 显示折扣推荐信息页面
	 * @access public
	 * @return 
	 */
	public function commendlistAction(){
		$discommObj = new Discommend();
		$discommArr = $discommObj->getAllDisCommend();
		$this->view->discommArr = $discommArr;
		$this->view->pageSection = $this->_getParam('p',0);
	}
	/**
	 * 显示添加折扣推荐信息页面
	 * @access public
	 * @return 
	 */
	public function commendaddAction(){
	
	}
	/**
	 * 添加折扣推荐信息
	 * @access public
	 * @return 
	 */
	public function commendinsAction(){
		$data['discid']	= $_REQUEST['discid'];
		$data['sdate']	= $_REQUEST['sdate'];
		$data['edate']	= $_REQUEST['edate'];
		$data['power']	= $_REQUEST['power'];
		$commendObj = new Discommend();
		$insid = $commendObj->insCommend($data);
	}
	/**
	 * 显示折扣推荐信息修改页面
	 * @access public
	 * @return 
	 */
	public function commendupdAction(){
		$disArr = $this->chkCommendId();
		$this->view->disArr = $disArr;
	}
	/**
	 * 修改折扣推荐信息
	 * @access public
	 * @return 
	 */
	public function commendmodiAction(){
		$id		= $_REQUEST['id'];
		$data['discid']	= $_REQUEST['discid'];
		$data['sdate']	= $_REQUEST['sdate'];
		$data['edate']	= $_REQUEST['edate'];
		$data['power']	= $_REQUEST['power'];
		$commendObj = new Discommend();
		$updcnt = $commendObj->updCommend($data,$id);
		$_SESSION['user']['msg'] = "修改成功";
		$this->_redirect("/admin/commendlist");
	}
	/**
	 * 删除折扣推荐信息
	 * @todo 应不能删除
	 * @access public
	 * @return 
	 */
	public function commenddelAction(){
	
	}
	/**
	 * 检查折扣信息ID
	 * @access private
	 * @return array 
	 */
	private function chkCommendId(){
		$commendid = $this->_getParam('id',null);
		if ($commendid == null) {
			$_SESSION['user']['msg'] = "";
			$this->_redirect("/admin/commendlist");
		}
		$commendObj = new Discommend();
		$disArr = $commendObj->getDisCommendById($commendid);
		return $disArr;
	}
	
	/**
	 * 显示折扣信息
	 * @access public
	 * @return 
	 */
	public function disclistAction(){
		$disObj = new Discount();
		$disListArr = $disObj->getAllDis();
		$this->view->disListArr = $disListArr;
		$this->view->pageSection = $this->_getParam('p',0);
	}
	/**
	 * 显示添加折扣信息页面
	 * @access public
	 * @return 
	 */
	public function discaddAction(){
	
	}
	/**
	 * 添加折扣信息
	 * @access public
	 * @return 
	 */
	public function discinsAction(){
		$data['tablename'] = $_REQUEST['tablename'];
		$data['objid'] = $_REQUEST['objid'];
		$data['state'] = $_REQUEST['state'];
		
		$data['coid'] = $_REQUEST['coid'];
		$data['coname'] = $_REQUEST['coname'];
		$data['distype'] = $_REQUEST['distype'];
		$data['disdetail'] = $_REQUEST['disdetail'];
		
		$data['disbrief1'] = $_REQUEST['disbrief1'];
		$data['disbrief2'] = $_REQUEST['disbrief2'];
		$data['dissdate'] = $_REQUEST['dissdate'];
		$data['disedate'] = $_REQUEST['disedate'];
		
		$data['dispicpath'] = $_REQUEST['dispicpath'];
		$data['comment'] = $_REQUEST['comment'];
		$data['clickcnt'] = $_REQUEST['clickcnt'];
		
		$discountObj = new Discount();
		$updcnt = $discountObj->insDiscount($data);
		$_SESSION['user']['msg'] = "修改成功";
		$this->_redirect("/admin/disclist");
	}
	/**
	 * 显示修改折扣信息页面
	 * @access public
	 * @return 
	 */
	public function discupdAction(){
		$discArr = $this->chkDiscId();
		$this->view->discArr = $discArr;
	}
	/**
	 * 修改折扣信息
	 * @access public
	 * @return 
	 */
	public function discmodiAction(){
		$discArr = $this->chkDiscId();
		$id = $_REQUEST['id'];
		$data['tablename'] = $_REQUEST['tablename'];
		$data['objid'] = $_REQUEST['objid'];
		$data['state'] = $_REQUEST['state'];
		
		$data['coid'] = $_REQUEST['coid'];
		$data['coname'] = $_REQUEST['coname'];
		$data['distype'] = $_REQUEST['distype'];
		$data['disdetail'] = $_REQUEST['disdetail'];
		
		$data['disbrief1'] = $_REQUEST['disbrief1'];
		$data['disbrief2'] = $_REQUEST['disbrief2'];
		$data['dissdate'] = $_REQUEST['dissdate'];
		$data['disedate'] = $_REQUEST['disedate'];
		
		$data['dispicpath'] = $_REQUEST['dispicpath'];
		$data['comment'] = $_REQUEST['comment'];
		$data['clickcnt'] = $_REQUEST['clickcnt'];
		
		$discountObj = new Discount();
		$updcnt = $discountObj->updDiscount($data,$id);
		$_SESSION['user']['msg'] = "修改成功";
		$this->_redirect("/admin/disclist");
	}
	/**
	 * 删除折扣信息
	 * @todo 一般不能删除
	 * @access public
	 * @return 
	 */
	public function discdelAction(){
	
	}
	/**
	 * 检查折扣信息ID
	 * @access private
	 * @return array 折扣信息数组
	 */
	private function chkDiscId(){
		if (!$this->_hasParam('id')) {
			$_SESSION['user']['msg'] = "";
			$this->_redirect("/admin/disclist");
		}
		$discid = $this->_getParam('id',null);
		$discObj = new Discount();
		$discArr = $discObj->getDiscountById($discid);
		return $discArr;
	}

	
	
	/***  以下为个人信息栏相关操作  ****/
	/**
	 * 显示修改资料页面
	 * @access public
	 * @return 
	 */
	public function updmeAction(){
		$this->view->userInfo = $_SESSION['user']['userinfo'];
	}
	/**
	 * 修改个人资料
	 * @access public
	 * @return 
	 */
	public function modimeAction(){
		$password = trim($_REQUEST['password']);
		if ((empty($password))||(md5($password) == $_SESSION['user']['userinfo']['password'])) {
			$updpwd = false;
		} else $updpwd = true;
		$truename = trim($_REQUEST['truename']);
		$sex = trim($_REQUEST['sex']);
		$birthday = trim($_REQUEST['birthday']);
		$dept = trim($_REQUEST['dept']);
		$position = trim($_REQUEST['position']);
		$position_desc = trim($_REQUEST['position_desc']);
		$office_phone = trim($_REQUEST['office_phone']);
		$mobile = trim($_REQUEST['mobile']);
		$home_phone = trim($_REQUEST['home_phone']);
		$email = trim($_REQUEST['email']);
		if ($updpwd) {
			$data = array(
				'password' =>$password ,
				'truename' => $truename,
				'sex' => $sex,
				'birthday' => $birthday,
				'dept' => $dept,
				'position' =>$position,
				'position_desc' => $position_desc,
				'office_phone' => $office_phone,
				'mobile' => $mobile,
				'home_phone' =>$home_phone,
				'email' => $email
			);
		} else { 
			$data = array(
				'truename' => $truename,
				'sex' => $sex,
				'birthday' => $birthday,
				'dept' => $dept,
				'position' =>$position,
				'position_desc' => $position_desc,
				'office_phone' => $office_phone,
				'mobile' => $mobile,
				'home_phone' =>$home_phone,
				'email' => $email
			);
		}
		$userObj = new User($_SESSION['user']['username']);
		$id = $_SESSION['user']['userinfo']['id'];
		$lastcnt = $userObj->updUser($data,$id);
		$_SESSION['user']['userinfo'] = $userObj->getUserInfo();
		$_SESSION['user']['msg'] = "修改成功!";
		$this->_redirect('/admin/updme');
	}
	
	/**
	 * 上传文件
	 * @access public
	 * @return 
	 */
	public function uploadAction(){
		if (isset($_REQUEST['postfile'])) {
			$this->view->post = true;
			$uploadfile = UPLOADIMG_DIR . $_FILES['postfile']['name'];
			if (move_uploaded_file($_FILES['postfile']['tmp_name'])) {
				$this->view->suc = true;
				$this->view->uploadfile = $uploadfile;
				$this->view->msgSection = "文件上传成功";
			} else {
				$this->view->suc = false;
				$this->view->msgSection = "文件上传失败,请重新上传";
			}
		} else {
			$this->view->post = false;
		}
	}
	
}